<template>
  <div class="patient-info">
    <div class="patient-info-inside" id="Add-verification-code">
      <div class="header">
        <div class="left item">
          <img alt="左侧" src="@/components/assets/images/design/1-12.webp" />
        </div>
        <div class="middle item">请输入您接收到的短信验证码</div>
        <div class="right item">
          <img alt="右侧" src="@/components/assets/images/design/1-12.webp" />
        </div>
      </div>
      <div class="patient-info-area">
        <div class="verificationCodeBox">
          <div class="InputBox">验证码：{{ value }}</div>
          <div
            class="btnBox"
            :class="{ disabled: isDisabled }"
            :style="{ cursor: isDisabled ? 'not-allowed' : 'pointer' }"
            @click="SendVoiceCode"
          >
            <!-- 电话语音接听验证码 -->
            {{ buttonText }}
          </div>
        </div>
        <van-number-keyboard
          :show="show"
          :hide-on-click-outside="false"
          extra-key="清空"
          @delete="delNumber"
          @input="addInput"
        />
        <div class="fontBtnBox">
          <div class="cancelBtn" @click="cancelBtn">取消</div>
          <div class="sureBtn" @click="sureBtn">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { NumberKeyboard, Toast } from "vant";
import { AndroidToWeb, buildParentApi } from "@/utils/designer/util";
import { v4 as uuid } from "uuid";
let buildParent = buildParentApi();
// import request from "@/services/axios";
export default {
  name: "AddVerificationCode",

  componentName: "AddVerificationCode",
  components: {
    NumberKeyboard,
    Toast,
  },
  props: {
    SelectTheCardsetupType: {
      type: String,
      default: "1",
    },
    mobileNum: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      show: true,
      value: "",
      isDisabled: false, // 控制按钮是否可点击
      countdown: 60, // 倒计时时间
      buttonText: "点击获取验证码",
    };
  },

  computed: {
    patientInfo() {
      return this.$store.getters["DRCbiz/getPatientInfo"];
    },
    cardTypeInfo() {
      return this.$store.getters["DRCbiz/getCardTypeInfo"];
    },
  },

  methods: {
    /**
     * @name 解析身份
     */
    parseUserMsg(data) {
      const obj = JSON.parse(data);
      const idNum = obj.IDNum;
      let birthday = "";
      if (idNum && idNum.length === 18) {
        // 提取出生年月日（格式：YYYY-MM-DD）
        const year = idNum.substring(6, 10);
        const month = idNum.substring(10, 12);
        const day = idNum.substring(12, 14);
        birthday = `${year}-${month}-${day}`;
      }
      const name = obj.Name;
      const gender = obj.Gender; // 如果Gender为空，可以根据身份证号判断性别
      let genderInfo = gender;
      if (!gender && idNum && idNum.length === 18) {
        // 身份证第17位为奇数表示男，偶数表示女
        const genderCode = parseInt(idNum.charAt(16), 10);
        genderInfo = genderCode % 2 === 1 ? "男" : "女";
      }
      return {
        name,
        sex: genderInfo,
        birthday,
        idNo: idNum,
        cardNo: data,
      };
    },
    /**
     * @name 发送语音
     */
    SendVoiceCode() {
      console.log(document.getElementById("Add-verification-code"));
      if (this.isDisabled) return; // 如果按钮已经禁用，则不执行操作
      this.isDisabled = true; // 禁用按钮
      this.startCountdown(); // 开始倒计时
      Toast({
        message: "发送成功",
        duration: 3000,
        position: "bottom",
        className: "succendToastBox",
        getContainer: () => document.getElementById("Add-verification-code"),
      });
    },
    startCountdown() {
      const timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--;
          this.buttonText = `${this.countdown}s后重新获取`;
        } else {
          clearInterval(timer); // 清除定时器
          this.isDisabled = false; // 恢复按钮可点击状态
          this.buttonText = "点击获取验证码"; // 恢复按钮文本
          this.countdown = 60; // 重置倒计时时间
        }
      }, 1000);
    },
    /**
     * @name 确定
     */
    sureBtn() {
      //     <Request>
      // <deviceIp>172.17.43.202</deviceIp>
      // <flowId>JWS30320250206090605919</flowId>
      // <timestamp>20250206090605919</timestamp>
      // <terminalNo>JWS303</terminalNo>
      // <method></method>
      // <idNo>411426202103110615</idNo>
      // <idType>29</idType>
      // <cardNo>{"code":0,"data":{"authNo":null,"bindAuthCode":null,"birthday":null,"chnlId":"1DBHJRS5I01E0F34A8C00000B959FA87","defPayChnlId":null,"ecIndexNo":"232EA7A88D3961BCC0772488AE616349018C1BBAB69CA9334C847AB474A489ED","ecQrCode":"4243918324133034905601410000","ecToken":"330000ecjcco5rpkle8998ab0a000091c6f872","email":null,"gender":null,"idNo":"411426202103110615","idType":"01","insuOrg":"411426","latlnt":null,"nationality":null,"showCodeType":null,"signCntrNo":null,"signFlag":null,"userName":"司铠博"},"message":"成功","orgId":"H33048300173"}</cardNo>
      // <cardType>29</cardType>
      // <setupType>1</setupType>
      // <patientType>医保</patientType>
      // <name>司铠博</name>
      // <sex>男</sex>
      // <birthday>2021-03-11</birthday>
      // <address />
      // <phone>18368348678</phone>
      // <pwd>{"code":0,"data":{"authNo":null,"bindAuthCode":null,"birthday":null,"chnlId":"1DBHJRS5I01E0F34A8C00000B959FA87","defPayChnlId":null,"ecIndexNo":"232EA7A88D3961BCC0772488AE616349018C1BBAB69CA9334C847AB474A489ED","ecQrCode":"4243918324133034905601410000","ecToken":"330000ecjcco5rpkle8998ab0a000091c6f872","email":null,"gender":null,"idNo":"411426202103110615","idType":"01","insuOrg":"411426","latlnt":null,"nationality":null,"showCodeType":null,"signCntrNo":null,"signFlag":null,"userName":"司铠博"},"message":"成功","orgId":"H33048300173"}</pwd>
      // <accountNo>20250206090605919</accountNo>
      // <cash>0.00</cash>
      // </Request>
      // let {} = this.patientInfo;
      // patientId": "4038986",
      //     "platformId": "YY0154331",
      //     "name": "卫婧怡",
      //     "nation": "汉族",
      //     "sex": "女",
      //     "birthday": "2004-06-01",
      //     "idNo": "330483200406010109",
      //     "cardNo": "{\"code\":0,\"data\":{\"authNo\":null,\"bindAuthCode\":null,\"birthday\":null,\"chnlId\":\"1DBHJRS5I01E0F34A8C00000B959FA87\",\"defPayChnlId\":null,\"ecIndexNo\":\"748E73E53CA69162585F14753329B860\",\"ecQrCode\":\"4243918369076438784001330000\",\"ecToken\":\"330000ecjd6s59rfif108fab0a000023daf293\",\"email\":null,\"gender\":null,\"idNo\":\"330483200406010109\",\"idType\":\"01\",\"insuOrg\":\"330483\",\"latlnt\":null,\"nationality\":null,\"showCodeType\":null,\"signCntrNo\":null,\"signFlag\":null,\"userName\":\"卫婧怡\"},\"message\":\"成功\",\"orgId\":\"H33048300173\"}",
      //     "cardType": "29",
      //     "cardStatus": "0",
      //     "address": "梧桐街道同心村",
      //     "phone": "18868044753",
      //     "patientType": "智慧医保",
      //     "createDate": "2025-02-06",
      //     "emergency": "0",
      //     "extend": "{\"idetinfo\":[],\"baseinfo\":
      let devInfo = JSON.parse(localStorage.getItem("deviceInfo"));
      // "{"CanOpen":true,"CardIDCode":"370300D15600000500991699E1F7D262","flag":"2.00","ylbxtcqbm":"370300","CardType":3,"CardNum":"C59055118","Gender":"","PsamTN":"000000000000","Success":true,"IDNum":"370306198701193612","Name":"朱坤"}"
      let parseInfo = this.parseUserMsg(this.cardTypeInfo.cardInfo);
      let cardId = this.cardTypeInfo.id;
      let patientType = cardId === 10 || cardId == 29 ? "医保" : "自费";
      let params = {
        Request: {
          deviceIp: devInfo.screenIp,
          flowId: uuid(),
          timestamp: this.$day().valueOf(),
          terminalNo: devInfo?.screenId, // 自助机设备终端	C	可空	根据医院实际情况
          method: "CreateCardAddPosit",
          // idNo:'',
          idType: this.cardTypeInfo.id, // 证件类别
          // cardNo:'',
          // cardType:'',
          setupType: this.SelectTheCardsetupType, // 建档类型	N	可空	1、本人 2、儿童 默认1
          patientType,
          // name:'',
          // sex:'',
          // birthday:'',
          // address:'',
          // phone:'',
          phone: this.mobileNum,
          // pwd: this.patientInfo.result.cardNo,
          pwd: parseInfo.cardNo,
          accountNo: "", // 交易账户	C	不可空	根据不同支付方式传入如院内账号就是卡号
          cash: "0.00", // cash	金额	C	不可空	发卡费
          // ...this.patientInfo.result,
          ...parseInfo,
        },
      };
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod(
        "axiosRep",
        paramsInfo,
        "CreateCardAddPosit"
      );
      // this.$emit("sureBtn", this.value);
    },
    /**
     * @name 取消
     */
    cancelBtn() {
      // request.get({
      //   url: "/sms/submit",
      //   params,
      // });
      let data = {
        ecName: "",
        apId: "",
        mobiles: "",
        content: "",
        sign: "",
        addSerial: "",
        mac: ""
      }
      //request.post({url: "/api/irms/modelpush/uploadScreenCurrentDataInfo", data})
      // this.$emit("cancelBtn", this.value);
    },
    /**
     * @name 删除按钮
     */
    delNumber() {
      this.value = this.value.slice(0, -1);
    },
    /**
     * @name 按钮输入
     */
    addInput(value) {
      if (value === "清空") {
        this.value = "";
      } else {
        this.value += value;
      }
    },
    handleSure() {
      this.$emit("handlePatientInfoSure");
    },
  },
};
</script>

<style scoped lang="scss">
@import "./assets/style";
</style>